﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TVDL4.VO;
using TVDL4.BUS;

namespace TVDL4
{
    public partial class frmVatLieu : frmParent  
    {
        private VatLieuBUS vatlieuBUS;
        private Module.ClickButtonStatus flagButton = Module.ClickButtonStatus.Ngung;//co phan biet dang o trang thai ghi hay ngung
        #region "event Method"
        public frmVatLieu()
        {
            InitializeComponent();
            vatlieuBUS = new VatLieuBUS();

        }
        private void flexVatlieu_AfterRowColChange(object sender, C1.Win.C1FlexGrid.RangeEventArgs e)
        {
            if (flexVatlieu.Selection.TopRow >= 1 && flexVatlieu.Rows[flexVatlieu.Selection.TopRow]["TenVatLieu"] != null)
            {
                txtTenvatlieu.Text = flexVatlieu.Rows[flexVatlieu.Selection.TopRow]["TenVatLieu"].ToString();
            }
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (flexVatlieu.Selection.TopRow >= 1 && flexVatlieu.Rows[flexVatlieu.Selection.TopRow]["KEYSL"].ToString() != "")
            {
                if (vatlieuBUS.DeleteRecord(flexVatlieu.Rows[flexVatlieu.Selection.TopRow]["KEYSL"].ToString()))
                {
                    flexVatlieu.Rows.Remove(flexVatlieu.Selection.TopRow);
                    ClearControl();
                }

            }
        }
        public void btnThem_Click(object sender, EventArgs e)
        {
            VatLieuVO vatlieuVO;
            switch (flagButton)
            {
                case Module.ClickButtonStatus.Ngung :
                     flagButton = Module.ClickButtonStatus.Them;
                     ClearControl();
                     break;
                case Module.ClickButtonStatus.Them:
                     vatlieuVO = new VatLieuVO(null,txtTenvatlieu.Text);
                     vatlieuVO = vatlieuBUS.InsertRecord(vatlieuVO);
                     if (vatlieuVO == null)
                         MessageBox.Show("Không ghi được dữ liệu", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                     else
                     {
                         flexVatlieu.Rows.Count++;
                         flexVatlieu.Rows[flexVatlieu.Rows.Count - 1]["STT"] = flexVatlieu.Rows.Count - 1;
                         flexVatlieu.Rows[flexVatlieu.Rows.Count - 1]["KEYSL"] = vatlieuVO.KeySL;
                         flexVatlieu.Rows[flexVatlieu.Rows.Count - 1]["TenVatLieu"] = vatlieuVO.TenVatLieu;
                         ClearControl();
                     }
                     flagButton = Module.ClickButtonStatus.Ngung;
                     break;
                case Module.ClickButtonStatus.Sua:
                    vatlieuVO = new VatLieuVO(flexVatlieu.Rows[flexVatlieu.Selection.TopRow]["KEYSL"].ToString(),txtTenvatlieu.Text);
                    vatlieuBUS.UpdateRecord(vatlieuVO);
                    UpdateControl();
                    flagButton = Module.ClickButtonStatus.Ngung;
                    break;


            }
            StatusButton();
          
            
        }
        private void btnSua_Click(object sender, EventArgs e)
        {

            switch (flagButton)
            {
                case Module.ClickButtonStatus.Ngung:
                    flagButton = Module.ClickButtonStatus.Sua;
                    break;
                case Module.ClickButtonStatus.Sua:
                    flagButton = Module.ClickButtonStatus.Ngung;
                    break;
                case Module.ClickButtonStatus.Them:
                    flagButton = Module.ClickButtonStatus.Ngung;
                    break;

            }
            StatusButton();
            
        }
        private void frmVatLieu_Load(object sender, EventArgs e)
        {
            flexVatlieu.Rows.Count = 1;
            List<VatLieuVO> lstVatLieu = vatlieuBUS.GetListVatLieu();
            foreach (VatLieuVO vatlieuVO in lstVatLieu)
            {
                flexVatlieu.Rows.Count++;
                flexVatlieu.Rows[flexVatlieu.Rows.Count - 1]["STT"] = flexVatlieu.Rows.Count-1;
                flexVatlieu.Rows[flexVatlieu.Rows.Count - 1]["KEYSL"] = vatlieuVO.KeySL;
                flexVatlieu.Rows[flexVatlieu.Rows.Count - 1]["TenVatLieu"] = vatlieuVO.TenVatLieu;
                
            }
        }
        #endregion

        #region "Private Method"
        private void ClearControl()
        {
            txtTenvatlieu.Text = "";
        }
        private void UpdateControl()
        {
            flexVatlieu.Rows[flexVatlieu.Selection.TopRow]["TenVatLieu"] = txtTenvatlieu.Text;
        }
        private void StatusButton()
        {
            if (flagButton == Module.ClickButtonStatus.Them || flagButton == Module.ClickButtonStatus.Sua )
            {
                btnThem.Text = "&Ghi";
                btnSua.Text = "&Ngưng";
                btnXoa.Enabled = false;
                btnThoat.Enabled = false;
                flexVatlieu.Enabled = false;
                txtTenvatlieu.Focus();
               
            }
            else
            {
                btnThem.Text = "&Thêm";
                btnSua.Text = "&Sửa";
                btnXoa.Enabled = true;
                btnThoat.Enabled = true;
                flexVatlieu.Enabled = true;
               
            }
        }
        
        #endregion

        

        

      
       

       


    }
}
